package com.hs.biye.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.biye.annotation.AuthCheck;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.constant.PositionConstant;
import com.hs.biye.constant.RolerConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.mapper.EmployeeMapper;
import com.hs.biye.model.DTO.DepartmentRequest;
import com.hs.biye.model.VO.DepartmentVo;
import com.hs.biye.model.entity.Department;
import com.hs.biye.model.entity.Employee;
import com.hs.biye.model.reslut.ResultPage;
import com.hs.biye.service.DepartmentService;
import com.hs.biye.mapper.DepartmentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
* @author haoshuai
*/
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
    implements DepartmentService{

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private EmployeeMapper employeeMapper;


    /**
     * 根据id获取部门信息
     * @param id
     * @return
     */
    @Override
    public DepartmentVo getOneDepartment(Integer id) {
        if (id == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Department department = getById(id);
        if (department == null){
            throw new BaseException(MessageConstant.NOT_QUERY);
        }
        Employee employee = employeeMapper.selectById(department.getDepartmentLeaderId());
        Long nums = employeeMapper.selectCount(new QueryWrapper<Employee>().eq("departmentId", department.getDepartmentId()));
        DepartmentVo departmentVo = new DepartmentVo();
        BeanUtils.copyProperties(department,departmentVo);
        //判断部门负责人
        departmentVo.setDepartmentLeaderName(employee!=null?employee.getEmployeeName():null);
        departmentVo.setDepartmentNums(nums.intValue());
        return departmentVo;
    }


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultPage<DepartmentVo> getDepartment(Integer page,Integer size) {
        IPage<Department> page2 = new Page<>(page ,size);
        departmentMapper.selectPage(page2, null);

        List<Department> records = page2.getRecords();
        long total = page2.getTotal();
        long current = page2.getCurrent();
        long pages = page2.getPages();

        List<DepartmentVo> list = new ArrayList<>();
        records.forEach(item->{
            Employee employee = employeeMapper.selectById(item.getDepartmentLeaderId());
            Long nums = employeeMapper.selectCount(new QueryWrapper<Employee>().eq("departmentId", item.getDepartmentId()));
            //转化Vo
            DepartmentVo departmentVo = new DepartmentVo();
            BeanUtils.copyProperties(item,departmentVo);
            departmentVo.setDepartmentLeaderName(employee==null?null:employee.getEmployeeName());
            departmentVo.setDepartmentNums(nums.intValue());
            list.add(departmentVo);
        });
        return ResultPage.success(list,total,current,pages);
    }


    /**
     * 获取部门名称和id
     * @return
     */
    public List<DepartmentVo> getDepartmentNameAndId(){
        LambdaQueryWrapper<Department> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.select(Department::getDepartmentId,Department::getDepartmentName);
        List<Department> departments = departmentMapper.selectList(objectLambdaQueryWrapper);
        List<DepartmentVo> departmentVos = new ArrayList<>();
        for (Department department:departments){
            DepartmentVo departmentVo = new DepartmentVo();
            BeanUtils.copyProperties(department,departmentVo);
            departmentVos.add(departmentVo);
        }
        return departmentVos;
    }


    /**
     * 获取部门名称和id
     * @return
     */
    public List<DepartmentVo> getDepartmentNameAndId(String name){
        LambdaQueryWrapper<Department> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.select(Department::getDepartmentId,Department::getDepartmentName);
        objectLambdaQueryWrapper.like(ObjectUtil.isNotEmpty(name),Department::getDepartmentName,name);
        List<Department> departments = departmentMapper.selectList(objectLambdaQueryWrapper);
        List<DepartmentVo> departmentVos = new ArrayList<>();
        for (Department department:departments){
            DepartmentVo departmentVo = new DepartmentVo();
            BeanUtils.copyProperties(department,departmentVo);
            departmentVos.add(departmentVo);
        }
        return departmentVos;
    }




    /**
     * 添加科室
     * @param
     * @return
     */
    @AuthCheck(mustRole = RolerConstant.ADMIN)
    @Override
    @Transactional
    public boolean addDepartment(DepartmentRequest departmentRequest) {
        //判断部门是否已经存在 如何存在抛出异常
        long count = count(new QueryWrapper<Department>().lambda().eq(Department::getDepartmentName,
                departmentRequest.getDepartmentName()));
        if (count != 0){
            throw new BaseException(MessageConstant.DEPARTMENT_EXIST);
        }
        Department department = new Department();
        department.setDepartmentName(departmentRequest.getDepartmentName());
        return save(department);
    }


    /**
     *修改科室
     * @param
     * @return
     */
    @AuthCheck(mustRole = RolerConstant.ADMIN)
    @Override
    @Transactional
    public boolean updateDepartment(DepartmentRequest departmentRequest) {

        //判断部门是否已经存在 如何存在抛出异常
        long count = count(new QueryWrapper<Department>().lambda().eq(Department::getDepartmentName,
                departmentRequest.getDepartmentName()));
        if (count != 0){
            throw new BaseException(MessageConstant.DEPARTMENT_EXIST);
        }
        Department department = new Department();
        department.setDepartmentId(departmentRequest.getDepartmentId());
        department.setDepartmentName(departmentRequest.getDepartmentName());
        return updateById(department);
    }


    /**
     * 删除科室
     * @param id
     * @return
     */
    @AuthCheck(mustRole = RolerConstant.ADMIN)
    @Override
    public boolean removeDepartment(Integer id) {
        if (id == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }

        //限制删除  todo 该部门有用户则不能删除
        return removeById(id);
    }
}




